from flask import Blueprint, render_template, request

from applications.common.curd import model_to_dicts
from applications.common.helper import ModelFilter
from applications.common.utils.http import table_api
from applications.common.utils.rights import authorize
from applications.models import CriminalInformaModels
from applications.common.utils.validate import xss_escape
from applications.schemas import CriminalInfoOutSchema
from datetime import datetime
from applications.common.utils.http import table_api, fail_api, success_api
from applications.extensions import db
from applications.common import curd


admin_criminal_info_bp = Blueprint('criminalInfo', __name__, url_prefix='/criminal/info')


@admin_criminal_info_bp.get('/')
# @authorize("criminal:info", log=True)
def main():
    return render_template('admin/admin_criminal_info.html')


@admin_criminal_info_bp.get('/data')
# @authorize("admin:monitor:equip", log=True)
def data():
    # 获取请求参数
    criminal_id = xss_escape(request.args.get('criminal_id', type=str))
    name = xss_escape(request.args.get('name', type=str))
    sex = xss_escape(request.args.get('sex', type=str))
    native_place = xss_escape(request.args.get('native_place', type=str))
    charge = xss_escape(request.args.get('charge', type=str))
    subordi_jur = xss_escape(request.args.get('subordi_jur', type=str))
    control_type = xss_escape(request.args.get('control_type', type=str))
    UWB_SN = xss_escape(request.args.get('UWB_SN', type=str))
    id = request.args.get('id', type=int)
    # 查询参数构造
    mf = ModelFilter()
    if name:
        mf.contains(field_name="name", value=name)
    if criminal_id:
        mf.contains(field_name="criminal_id", value=criminal_id)
    if id:
        mf.exact(field_name="id", value=id)
    if sex:
        mf.exact(field_name='sex', value=sex)
    if native_place:
        mf.exact(field_name='native_place', value=native_place)
    if charge:
        mf.exact(field_name='charge', value=charge)
    if control_type:
        mf.exact(field_name='control_type', value=control_type)
    if subordi_jur:
        mf.exact(field_name='subordi_jur', value=subordi_jur)
    if UWB_SN:
         mf.exact(field_name='UWB_SN', value=UWB_SN)
        # if tranning_state == 2:mf.exact(field_name='tranning_state_desc',value='已完成')
    # orm查询
    # 使用分页获取data需要.items
    criminalModel = CriminalInformaModels.query.filter(mf.get_filter(model=CriminalInformaModels)).layui_paginate()
    count = criminalModel.total
    # 返回api
    return table_api(data=model_to_dicts(schema=CriminalInfoOutSchema, data=criminalModel.items), count=count)


# 罪犯信息详情
@admin_criminal_info_bp.get('/details/<int:id>')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def criminal_list_details(id):
    criminalModel = curd.get_one_by_id(CriminalInformaModels, id)
    return render_template('admin/admin_criminal_info_detail.html', criminalM=criminalModel)

# 罪犯信息添加
@admin_criminal_info_bp.get('/adds')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def criminal_list_adds():
    return render_template('admin/admin_criminal_info_add.html')

# 罪犯信息保存
@admin_criminal_info_bp.post('/save')
# @authorize("aadmin:monitor:equip:add", log=True)
def save():
    req_json = request.json
    criminal_id = xss_escape(req_json.get('criminal_id'))
    name = xss_escape(req_json.get('name'))
    sex = xss_escape(req_json.get('sex'))
    native_place = xss_escape(req_json.get('native_place'))
    charge = xss_escape(req_json.get('charge'))
    subordi_jur = xss_escape(req_json.get('subordi_jur'))
    control_type = xss_escape(req_json.get('control_type'))
    UWB_SN = xss_escape(req_json.get('UWB_SN'))
    GPS_SN = xss_escape(req_json.get('GPS_SN'))
    identify = xss_escape(req_json.get('identify'))
    adress = xss_escape(req_json.get('adress'))
    create_time = datetime.now().replace(microsecond=0)


    if not criminal_id or not name or not subordi_jur:
        return fail_api(msg="不得为空")

    equipModel = CriminalInformaModels(criminal_id=criminal_id, name=name,sex=sex,native_place=native_place,charge=charge,subordi_jur=subordi_jur,
                                       control_type=control_type,UWB_SN=UWB_SN,GPS_SN=GPS_SN,identify=identify,adress=adress,
                                       create_time=create_time)
    db.session.add(equipModel)
    db.session.commit()
    return success_api(msg="增加成功")